package com.hsy.swu.home.course

import com.hsy.swu.MyConfig
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter

class SimpleCourseModel {
    var courseName: String = ""
    var classRoom: String = ""
    var studyWeek: String = ""
    var dayOfWeek: Int = 0
    var time: String = ""

    override fun toString() = "$courseName\t$classRoom\t$studyWeek\t$dayOfWeek\t$time"

    private fun isInWeek(currentWeek: Int): Boolean {
        var week: String = studyWeek.replace("周", "") // 先把周字删掉
        // TODO start 可能要改单双周的逻辑
        week = week.replace("(单)", "")
        week = week.replace("(双)", "")
        // TODO end
        week.split(',').forEach {
            val isInWeekResult = if (it.contains("-")) {
                currentWeek in it.split("-")[0].toInt()..it.split("-")[1].toInt()
            } else {
                currentWeek == it.toInt()
            }
            if (isInWeekResult) return true
        }
        return false
    }

    /** 学习日是今天且在本周有课，用来判断是否需要展示在 courseWidget上 */
    fun isTodayCourseAndInWeek(): Boolean {
        MyConfig.update()
        return LocalDate.now().dayOfWeek.value == dayOfWeek && isInWeek(MyConfig.currentWeek)
    }

    /** 有课且马上要上了，用来判断是否需要 postNotification */
    fun isSoonBegin(): Boolean {
        val startTimeString = time.split("-")[0]
        val startTime = LocalTime.parse(startTimeString, DateTimeFormatter.ofPattern("HH:mm"))
        return isTodayCourseAndInWeek()
                && LocalTime.now().isAfter(startTime.minusHours(1))
                && LocalTime.now().isBefore(startTime)
    }

    companion object {
        fun toSimpleCourseModelList(list: List<CourseModel>): List<SimpleCourseModel> {
            val resultList = mutableListOf<SimpleCourseModel>()
            list.forEach {
                resultList.add(
                    SimpleCourseModel().apply {
                        courseName = it.courseName
                        classRoom = it.classRoom
                        studyWeek = it.studyWeek
                        dayOfWeek = it.dayOfWeek

                        val startSection = it.startSection
                        val endSection = it.endSection
                        time = "${CourseModel.sectionTimeMap[startSection]!!}" +
                                "-" +
                                "${CourseModel.sectionTimeMap[endSection]!!.plusMinutes(45)}"
                    }
                )
            }
            return resultList
        }

        fun getShowOnWidgetList(list: List<SimpleCourseModel>): List<SimpleCourseModel> {
            val resultList = mutableListOf<SimpleCourseModel>()
            list.forEach {
                if (it.isTodayCourseAndInWeek()) {
                    resultList.add(it)
                }
            }
            return resultList
        }

        fun getSoonBeginCourse(list: List<SimpleCourseModel>): SimpleCourseModel? {
            var soonBeginCourse: SimpleCourseModel? = null
            list.forEach {
                if (it.isSoonBegin()) {
                    soonBeginCourse = it
                }
            }
            return soonBeginCourse
        }
    }
}